package com.cs.service;

import com.cs.mapper.MenuMapper;
import com.cs.pojo.Menu;
import com.cs.pojo.MenuExample;
import com.cs.pojo.Role;
import com.cs.pojo.RoleExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MenuService {
    @Resource
    private MenuMapper menuMapper;
    public List<String> queryByRId(int roleId){
        List<String> strings = menuMapper.selectByRId(roleId);
        return strings;
    }

    public List<Menu> queryAll(){
        List<Menu> menuList = menuMapper.selectByExample(null);
        return menuList;
    }

    public List<Menu> queryStateList(){
        MenuExample example=new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();
        criteria.andMStateEqualTo(0);
        List<Menu> menuList = menuMapper.selectByExample(example);
        return menuList;
    }

    public PageInfo<Menu> queryAll2(String menuname, int pageNum, int pageSize){
        MenuExample example=new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();
        if (menuname!=null){
            if (!menuname.trim().equals("")){
                criteria.andMNameLike("%"+menuname.trim()+"%");
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Menu> menus = menuMapper.selectByExample(example);
        return new PageInfo<>(menus);
    }

    public Menu queryById(int mId){
        Menu menu = menuMapper.selectByPrimaryKey(mId);
        return menu;
    }

    public int update(Menu menu){
        int i = menuMapper.updateByPrimaryKeySelective(menu);
        return i;
    }

    public int deleteOne(int mId){
        Menu menu=new Menu();
        menu.setmId(mId);
        menu.setmState(2);
        int i = menuMapper.updateByPrimaryKeySelective(menu);
        return i;
    }

    public int add(Menu menu){
        int insert = menuMapper.insert(menu);
        return insert;
    }

}
